﻿namespace DotRas
{
    using System;
    using System.Runtime.InteropServices;
    using System.Security;

    [SuppressUnmanagedCodeSecurity]
    internal class UnsafeNativeMethods
    {
        private UnsafeNativeMethods()
        {
        }

        [DllImport("kernel32.dll")]
        public static extern void CopyMemory(IntPtr destination, IntPtr source, IntPtr length);
        public static int DeleteEntry(string lpszPhonebook, string lpszEntryName)
        {
            return RasDeleteEntry(lpszPhonebook, lpszEntryName);
        }

        public static int EnumAutodialAddresses(IntPtr lppAddresses, ref IntPtr lpCb, ref IntPtr lpcAddresses)
        {
            return RasEnumAutodialAddresses(lppAddresses, ref lpCb, ref lpcAddresses);
        }

        public static int GetAutodialAddress(string lpszAddress, IntPtr lpdwReserved, IntPtr lpAddresses, ref IntPtr lpCb, ref IntPtr lpcEntries)
        {
            return RasGetAutodialAddress(lpszAddress, lpdwReserved, lpAddresses, ref lpCb, ref lpcEntries);
        }

        public static int GetAutodialEnable(int dwDialingLocation, ref bool lpfEnabled)
        {
            return RasGetAutodialEnable(dwDialingLocation, ref lpfEnabled);
        }

        public static int GetAutodialParam(DotRas.NativeMethods.RASADP dwKey, IntPtr lpvValue, ref int lpdwcbValue)
        {
            return RasGetAutodialParam(dwKey, lpvValue, ref lpdwcbValue);
        }

        public static int GetCredentials(string lpszPhonebook, string lpszEntryName, IntPtr lpCredentials)
        {
            return RasGetCredentials(lpszPhonebook, lpszEntryName, lpCredentials);
        }

        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        private static extern int RasDeleteEntry(string lpszPhonebook, string lpszEntryName);
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("rasdlg.dll", CharSet=CharSet.Unicode)]
        public static extern bool RasDialDlg(string lpszPhoneBook, string lpszEntryName, string lpszPhoneNumber, ref DotRas.NativeMethods.RASDIALDLG lpInfo);
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("rasdlg.dll", CharSet=CharSet.Unicode)]
        public static extern bool RasEntryDlg(string lpszPhonebook, string lpszEntryName, ref DotRas.NativeMethods.RASENTRYDLG lpInfo);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        private static extern int RasEnumAutodialAddresses([In, Out] IntPtr lppAddresses, ref IntPtr lpCb, ref IntPtr lpcAddresses);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        public static extern int RasEnumEntries(IntPtr reserved, string lpszPhonebook, [In, Out] IntPtr lpRasEntryName, ref IntPtr lpCb, ref IntPtr lpcEntries);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        private static extern int RasGetAutodialAddress(string lpszAddress, IntPtr lpdwReserved, [In, Out] IntPtr lpAddresses, ref IntPtr lpCb, ref IntPtr lpcEntries);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        private static extern int RasGetAutodialEnable(int dwDialingLocation, [MarshalAs(UnmanagedType.Bool)] ref bool lpfEnabled);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        private static extern int RasGetAutodialParam(DotRas.NativeMethods.RASADP dwKey, IntPtr lpvValue, ref int lpdwcbValue);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        private static extern int RasGetCredentials(string lpszPhonebook, string lpszEntryName, [In, Out] IntPtr lpCredentials);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        public static extern int RasGetEntryProperties(string lpszPhonebook, string lpszEntryName, [In, Out] IntPtr lpRasEntry, ref IntPtr dwEntryInfoSize, IntPtr lpbDeviceInfo, IntPtr dwDeviceInfoSize);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        public static extern int RasGetSubEntryProperties(string lpszPhonebook, string lpszEntryName, int dwSubEntry, [In, Out] IntPtr lpRasSubEntry, ref IntPtr lpCb, IntPtr lpbDeviceConfig, IntPtr lpcbDeviceConfig);
        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("rasdlg.dll", CharSet=CharSet.Unicode)]
        public static extern bool RasPhonebookDlg(string lpszPhonebook, string lpszEntryName, ref DotRas.NativeMethods.RASPBDLG lpInfo);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        public static extern int RasRenameEntry(string lpszPhonebook, string lpszOldEntryName, string lpszNewEntryName);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        public static extern int RasSetAutodialAddress(string lpszAddress, int dwReserved, IntPtr lppAddresses, int lpCb, int lpcEntries);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        public static extern int RasSetAutodialEnable(int dwDialingLocation, [MarshalAs(UnmanagedType.Bool)] bool fEnabled);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        public static extern int RasSetAutodialParam(DotRas.NativeMethods.RASADP dwKey, IntPtr lpvValue, int dwcbValue);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        private static extern int RasSetCredentials(string lpszPhonebook, string lpszEntryName, IntPtr lpCredentials, [MarshalAs(UnmanagedType.Bool)] bool fClearCredentials);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        private static extern int RasSetEntryProperties(string lpszPhonebook, string lpszEntryName, IntPtr lpRasEntry, int dwEntryInfoSize, IntPtr lpbDeviceInfo, int dwDeviceInfoSize);
        [DllImport("rasapi32.dll", CharSet=CharSet.Unicode)]
        public static extern int RasSetSubEntryProperties(string lpszPhonebook, string lpszEntryName, int dwSubEntry, IntPtr lpRasSubEntry, int dwcbRasSubEntry, IntPtr lpbDeviceConfig, int dwcbDeviceConfig);
        public static int SetCredentials(string lpszPhonebook, string lpszEntryName, IntPtr lpCredentials, bool fClearCredentials)
        {
            return RasSetCredentials(lpszPhonebook, lpszEntryName, lpCredentials, fClearCredentials);
        }

        public static int SetEntryProperties(string lpszPhonebook, string lpszEntryName, IntPtr lpRasEntry, int dwEntryInfoSize, IntPtr lpbDeviceInfo, int dwDeviceInfoSize)
        {
            return RasSetEntryProperties(lpszPhonebook, lpszEntryName, lpRasEntry, dwEntryInfoSize, lpbDeviceInfo, dwDeviceInfoSize);
        }
    }
}

